<?php
require_once 'model/db.php';
?>
<?php
$query = "SELECT * FROM companies";
$companies = getCompanies("SELECT * FROM companies");
$apps = getApps("SELECT * FROM apps");
$pages = getPages($query);
?>

<form method="POST">
    Min fans<input type="text" name="min_fans"><br>
    Max fans<input type="text" name="max_fans"><br>
    Keyword <input type="text" name="keyword"><br>
    Company<select name="id_company">
        <?php foreach ($companies as $company): ?>
            <option value="<?php echo $company['id'] ?>"><?php echo $company['name'] ?></option>
        <?php endforeach; ?>
    </select>
    Apps <select name="App">
        <?php foreach ($apps as $app): ?>
            <option value="<?php echo $app['pid'] ?>"><?php echo $app["name"] ?></option>
        <?php endforeach; ?>
    </select><br>
    <input type="submit" name="submit">
</form>
<?php if (isset($_POST['submit'])): ?>
    <?php
    $min_fans = $_POST['min_fans'];
    $max_fans = $_POST['max_fans'];
    $keyword = $_POST['keyword'];
    $company=$_POST["id_company"];
    $apps=$_POST["id_apps"];
    $query = "SELECT * FROM pages 
                  
                  WHERE (likes <= $max_fans)
                      AND (likes >= $min_fans)
                      AND (description LIKE '% $keyword %')
                  LIMIT 0, 30
                  ";
    $pages = getCompanies($query);
    ?>
    <?php if (!empty($pages)): ?>
        <table id="table1" border="1px">
            <tr>
                <td> STT </td>
                <td> Id </td>               
                <td> Name </td>
                <td> Picture </td>
                <td> likes </td>
                <td> Talking </td>
                <td> category </td>
                <td> link </td>
                <td> website </td>
                <td> description </td>
            </tr>
            <?php foreach ($pages as $page): ?>
                <tr>
                    <td><?php echo $page["pid"] ?></td>
                    <td><?php echo $page["id"] ?> </td>                    
                    <td><?php echo $page["name"] ?></td>
                    <td><img src="<?php echo $page['picture'] ?>"></td>
                    <td><?php echo $page['likes'] ?></td>
                    <td><?php echo $page["talking_about_count"] ?></td>
                    <td><?php echo $page["category"] ?></td>
                    <td><a href='<?php echo $page["link"] ?>'><?php echo $page["link"] ?></td>
                    <td><a href='<?php echo $page["website"] ?>'><?php echo $page["website"] ?></td>
                    <td class="description"><?php echo $page["description"] ?></td>
                </tr>
            <?php endforeach; ?>
        </table> 

    <?php endif; ?>
<?php endif; ?>
